//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 08/05/2005
// Last Modif: 09/05/2005
//


var MOD_CLA = {}



// Navigation buttons


// -----------------------------------------------------
// | claim | D E                               S X |    |
// -----------------------------------------------------
// | TD11  |    TD12                               |TD13|
// | TD21  |    TD22                               |    |
// |----------------------------------------------------


MOD_CLA.onsplitf=function(cl,tf) { 
  var txtarea=document.getElementById("tftxtarea"+cl+"_"+tf);  
  var cursorat=html_getCursorPos(txtarea);

  if(!cursorat) {
    return;
  }

  var txt1=txtarea.value.substring(0,cursorat);
  var txt2=txtarea.value.substring(cursorat,txtarea.value.length);
  
  // put glue everywhere
  var cltf_iter= featuresplit(cl,tf,txt1,txt2,"clarity").iterator();
  var warnings= new List();
  
  while(!cltf_iter.end()) {
    var ctf=new claim_tf(cltf_iter.getValue());
    if(GetFeatureWarn(ctf.getcl(),ctf.gettf())){
      warnings.push(cltf_iter.getValue());
      html_setwarning("tfwarn"+ctf.getcl()+"_"+ctf.gettf(),GetFeatureWarn(ctf.getcl(),ctf.gettf()));
    }
    if(IsClaimInPage(ctf.getcl())) { // && is_Tainted(clo)) { 	 
      tf_updatetfTD11(ctf.getcl(),ctf.gettf());   
    }
    cltf_iter.next();
  }
    

  if(warnings.length()) {
    alert("Check references to "+cl+"."+tf+" in : "+warnings.getValue());
  }

  // Update view

  var oldrow=document.getElementById("clrow"+cl+"_"+tf);
  if(oldrow) {
    var bigt=document.getElementById("bigcltable");
    var insertidx=document.getElementById("clrow"+cl+"_"+tf).sectionRowIndex;
    var newrow,newcell;
   
   // each TF entry comprises two lines with 3 cells 
    newrow=bigt.insertRow(insertidx);
 
    newrow.insertCell(0).className="NOTOP";
    newcell=newrow.insertCell(1);
    newcell.valign='bottom';
    newcell.className="NOTOP";
    newrow.insertCell(2).rowspan=2;
   
    newrow=bigt.insertRow(insertidx);
    newrow.className="NOBOTTOM";
    newrow.insertCell(0).className="NOBOTTOM";
    newrow.insertCell(1).className="NOBOTTOM";
    newrow.insertCell(2);

    tf_updadetable(cl,tf,insertidx);       
  }  
}


MOD_CLA.onjointf=function(cl,tf) {  
  var checklist=featuresjoin(cl,tf); // do the join at the tf level
  var cltf_iter= checklist.iterator();
  var warnings= new List();

  while(!cltf_iter.end()) {
    var ctf=new claim_tf(cltf_iter.getValue());
    if(GetFeatureWarn(ctf.getcl(),ctf.gettf())){
      warnings.push(cltf_iter.getValue());
      html_setwarning("tfwarn"+ctf.getcl()+"_"+ctf.gettf(),GetFeatureWarn(ctf.getcl(),ctf.gettf()));
    }
    if(IsClaimInPage(ctf.getcl())) { // && is_Tainted(clo)) { 	 
      tf_updatetfTD11(ctf.getcl(),ctf.gettf());   
    }
    cltf_iter.next();
  }

  if(warnings.length()) {
    alert("Check references to "+cl+"."+tf+" in : "+warnings.getValue());
  }
  
  // overwritten hint may have changed 
  var oldrow=document.getElementById("clrow"+cl+"_"+tf);
  if(oldrow) {
    var bigt=document.getElementById("bigcltable");
    var insertidx=document.getElementById("clrow"+cl+"_"+tf).sectionRowIndex;
    bigt.deleteRow(insertidx);
    bigt.deleteRow(insertidx);
    tf_updadetable(cl);    
    
    // update autovalue for all features below tf, for all documents 
    var tf_iter= new claim_features_iterator(cl,"",tf);
    while(!tf_iter.end()) {
      featureupdatevalfromautoall(tf_iter.getcl(),tf_iter.gettf()); // update forall docs 
      tf_updatevalfromauto(tf_iter.getcl(),tf_iter.gettf(),MOD_CTF.seldoc); // update only current view
      tf_iter.next();
    }

  }
}


MOD_CLA.showtfTD11=function(cl,tf,tf_iter) {
  var eqclass="";
  var eqsize=6;

  var tfeq=tf_iter.getEq();

  if(tfeq) {   
    eqclass="eqtf";
    tfopts = " disabled";
  }     

  // make space for import tf 
  if(GetClaimSetVersion()>1) { 
    eqsize=1;
  }
  
  var html=new Array();
  var hi=0;
  //html.push("<fieldset><legend><b>TF "+cl+"."+tf+"</b></legend>");
  html[hi++]="<TABLE border=1 class='"+eqclass+"' width=100% id='tabtf"+cl+"_"+tf+"'>";
  html[hi++]="<TR><TD align=center><b>TF "+cl+"."+tf+"</b></TD></TR><TR>";

  // TF EQ
  var eqtag="<b>E</b> ";
  var eqtxt="";
  
  if(tfeq) {
    var eqtf=new claim_tf(tfeq);
     eqtag+="<input type=text size="+eqsize+" value='"+tfeq+"' ";
     if(is_claim_tf(eqtf.getcl(),eqtf.gettf())) {
      eqtxt=GetFeatureDesc(eqtf.getcl(),eqtf.gettf());
     } else {
      eqtxt+="Unknown feature "+tfeq;
     }
  } else {
    eqtag+="<input type=text size="+eqsize+" value='' ";
  }
  eqtag  +="onChange='MOD_CLA.tf_onchangetfeq("+cl+","+tf+",this.value);' id='tfeq"+cl+"_"+tf+"'>";
  html[hi++]="<TD>"+html_whatis(eqtag,eqtxt,"whatEQ"+cl+"_"+tf);
  html[hi++]=html_warning("tfwarn"+cl+"_"+tf,GetFeatureWarn(cl,tf),"tf_onresettfwarn("+cl+","+tf+")");
  
  // TF Import 
  if(GetClaimSetVersion()>1) { 
    var imptf=GetFeatureImport(cl,tf);
    if(imptf) {
      var vtf=new claim_vtf(imptf);
      var whatag =" I <input type=text id='tfimp"+cl+"_"+tf+"' size="+(eqsize+2)+" ";
      whatag+="value='"+imptf+"' onChange='tf_onchangetfimp("+cl+","+tf+",this.value);'>";
      html[hi++]="<b>"+html_whatis(whatag,GlobalGetFeatureAttr(vtf.getver(),vtf.getcl(),vtf.gettf(),"desc"),
				   "whatIMP"+cl+"_"+tf)+"</b>";
    } else {
      var whatag =" I <input type=text id='tfimp"+cl+"_"+tf+"' size="+(eqsize+2)+" ";
      whatag+="value='' onChange='tf_onchangetfimp("+cl+","+tf+",this.value);'>";
      
      html[hi++]="<b>"+html_whatis(whatag,"","whatIMP"+cl+"_"+tf)+"</b>";
    }
    //html[hi++]=" I <input type=text id='tfimp"+cl+"_"+tf+"' size="+(eqsize+2)+" ";
    //html[hi++]="value='"+imptf+"' onChange='tf_onchangetfimp("+cl+","+tf+",this.value);'>";
  }	
  html[hi]="</TD></TR></TABLE>";
  return html.join('');
}

MOD_CLA.showtfTD12=function(cl,tf,tf_iter,eq) {
  return html_splitflexarea(tf_iter.getDesc(),
			    "tf_onchangefeature("+cl+","+tf+");",
			    "class='"+getFeatureClass(cl,tf)+"' id='tftxtarea"+cl+"_"+tf+"'",
			    64,2);  
}


MOD_CLA.showtfTD13=function(cl,tf) {  
  var html="<TABLE height=100%><TR><TD valign=top>";
  if(tf==1) {
    if(claim_isdependent(cl)) { // use pure dependency
      if(!GetClaimHeader(cl)) {
	html+="<button onClick='tf_onstripheader("+cl+");'>"+
	  html_img(striphead_img,""," ALT='Strip header at cursor'")+"</button>";
      } else {
	html+="<button onClick='tf_onjoinheader("+cl+")'>"+
	  html_img(jointf_img,"","ALT='Undo strip header'")+"</button>";	
      }
    }
  } else {
    html+="<button onClick='MOD_CLA.onjointf("+cl+","+tf+");'>"+html_img(jointf_img)+"</button>";
  };
  html+="</TD><TR><TD valign=bottom>";
  html+="<button onClick='MOD_CLA.onsplitf("+cl+","+tf+");'>"+html_img(splittf_img)+"</button>";
  html+="</TD></TR></TABLE>";
  return html;
}



MOD_CLA.showtfTD21=function(cl,tf,tf_iter,eq) {
  var html="";
  // TF Clarity   
  var unclar=tf_iter.getUnclarityEq();//GetFeatureUnclarityEq(cl,tf);
  if(unclar) {
    // eval("clar_epc_"+unclar+"("+cl+")")
    var preview=stdcls_preview("clarity",unclar,cl,tf_iter.getDesc(),GetDossierLang(),GetClaimSetLaw());
    //alert("clarity "+unclar+" "+cl+" "+tf_iter.getDesc()+" =>"+preview);
    html=html_whatis("<b> C </b> ",preview,"whatCLAR"+cl+"_"+tf);
  } else {
    html=html_whatis("<b> C </b> ","","whatCLAR"+cl+"_"+tf);
  }
  html+=html_selection("clar"+cl+"_"+tf,CLAR_HINTS,
		       unclar,
		       "tf_onchangetfclar("+cl+","+tf+");",
		       tf_iter.getEq() ? " disabled " : "");
  
  return html;
}




MOD_CLA.showtfTD22=function(cl,tf,tf_iter,eq) {
  return "";
}

MOD_CLA.tf_onchangeeq=function(cl,eqcl){
  new_Taint(cl); 
  changeEq(cl,eqcl); 
  html_setwhatis("whatclEQ"+cl,eqcl ? GetClaimDescription(eqcl) : "");
  html_setwarning("cleqwarn"+cl,GetClaimEqWarn(cl));
  html_setwarning("clgrpwarn"+cl,GetClaimGroupWarn(cl));
  html_setwarning("allwarn",(GetClaimsWarnings() >0 ) ? GetClaimsWarnings()+": warnings":"");
  tf_updatenotsearchedview();
}

MOD_CLA.tf_onchangetfeq=function(cl,tf,val){
  if(!featurechangeeq(cl,tf,val)) {
    document.getElementById("tfeq"+cl+"_"+tf).value=GetFeatureEq(cl,tf);
    return;
  }  
  if(GetFeatureWarn(cl,tf)) {
    tf_onresettfwarn(cl,tf);
  }
  
  tf_updatevalfromauto(cl,tf,MOD_CTF.seldoc);
  //tf_updatetfTD11(cl,tf);  
  //tf_updatetfTD21(cl,tf);  
  //tf_updatetfTD22(cl,tf,MOD_CTF.seldoc);
}


//------------------------------------------------------------------------------
//
// render info for one claim , for one doc 
//

MOD_CLA.showalltf=function(cl) {
  var eq=GetClaimEq(cl);
  var html=new Array();
  var hi=0;
  var tf_iter= new claim_features_iterator(cl);
  
  if(GetClaimHeader(cl)) {
    html[hi++]="<TR><TD></TD><TD>"+ html_flexarea(GetClaimHeader(cl),"tf_onchangeheader("+cl+");",
						  "class='ROTXT' id='tftxtarea"+cl+"_0'",
						  64)+"</TD></TR>";      
  }
  while(!tf_iter.end()) { 
    var tfeq=tf_iter.getEq();
    var tf=tf_iter.getValue();
    html[hi++]="<TR id='clrow"+cl+"_"+tf+"'>";
    html[hi++]="<TD style='border-bottom-width:0;'>"+MOD_CLA.showtfTD11(cl,tf,tf_iter)+"</TD>";  
    html[hi++]="<TD valign=bottom style='border-bottom-width:0;'>"+MOD_CLA.showtfTD12(cl,tf,tf_iter,eq)+"</TD>";
    html[hi++]="<TD valign=top rowspan=2>"+MOD_CLA.showtfTD13(cl,tf)+"</TD>";  
    
    html[hi++]="</TR><TR style='border-top-width:0;'>"; 
    html[hi++]="<TD style='border-top-width:0;'>"+MOD_CLA.showtfTD21(cl,tf,tf_iter,eq)+"</TD>"; 
    html[hi++]="<TD style='border-top-width:0;' id='tdtfeq"+cl+"_"+tf+"'>"+MOD_CLA.showtfTD22(cl,tf,tf_iter,eq)+"</TD>";
    html[hi++]="<TD align=left valign=bottom ></TD></TR>";      
    tf_iter.next();
  }
  
  return html.join('');
}

MOD_CLA.FULLVIEW=new Array();
MOD_CLA.swfullview=function(cl,v) {
  MOD_CLA.FULLVIEW[cl]=v;
  edit_clarity();
}
   

MOD_CLA.editclarcl=function(cl) {
  var warnclass="HIDEME";
  var clclass=ClaimTypeStyle(cl);
  var html=new Array();
  
  var eq=GetClaimEq(cl);
  html.push("<TR><TD id='fdcltd"+cl+"' class='"+clclass+"' style='border-top-width:2;'>");
  html.push("<input type=button id='fdbcltd"+cl+"'  class='"+clclass+"'");
  html.push("value='Claim "+cl+"' onClick=''>");
  html.push("</TD><TD style='border-top-width:2;'><TABLE border=0 width=100%><TR>");
  html.push("<TD align=left>");
  // Claim DEP
  html.push("<b>D</b> <input type=text " );
  html.push(" class=DEPEQ align=center disabled value='"+GetClaimDep(cl)+"' id='DEP"+cl+"'> ");
  // Claim EQ
  if(eq) {
    html.push(html_whatis("<b>E</b>",GetClaimDescription(eq),"whatclEQ"+cl));
  } else {
    html.push(html_whatis("<b>E</b>","","whatclEQ"+cl));
  }

  html.push(" <input type=text  class=DEPEQ value='"+eq+"' ");
  html.push(" id=EQ"+cl+" onFocus='MOD_CTF.EQFOCUS="+cl+";' onChange='MOD_CLA.tf_onchangeeq("+cl+",this.value);'>");
  html.push(" "+html_warning("cleqwarn"+cl,GetClaimEqWarn(cl),"tf_onresetcleqwarn("+cl+")"));
  
  // Claim Group
  if(GetGroupsLength()>1) {
    html.push(" <b>G</b>"+group_select(cl,"tf_onchangeclgroup"));
    // Warn Info
    html.push(" "+html_warning("clgrpwarn"+cl,GetClaimGroupWarn(cl),"tf_onresetclgrpwarn("+cl+")"));
  }

  html.push("</TD>");
  html.push("<TD align=center class='CLHEADER' style='border-top-width:2;'>Feature Description</TD>");

  html.push("<TH>"+gclarselection(cl,"","edit_clarity()")+"</TH>");

  html.push("</TR></TABLE></TD>");
 
  // Show/ Hide full claim features (including feature from parent claims)
  if(claim_isdependent(cl)) { // pure dep
    html.push("<TD align=center style='border-top-width:2;'>");        
    if(MOD_CTF.FULLVIEW[cl]) {           
      html.push("<button onclick='MOD_CTF.tf_swfullview("+cl+",0);'>"+html_img(fullview_img)+"</button></TD>");
    } else {
      html.push("<button onclick='MOD_CTF.tf_swfullview("+cl+",1);'>"+html_img(compactview_img)+"</button></TD>");
    }
  }
  html.push("</TR>");

  if(MOD_CLA.FULLVIEW[cl]) {
    var uhtml="";
    var clup=GetClaimDep(cl);
    while(dep_isdependent(clup)) {
      uhtml=MOD_CLA.showalltf(clup,"F")+uhtml;
      clup=GetClaimDep(clup);
    }
    html.push(uhtml);
  } 
  html.push(MOD_CLA.showalltf(cl,"C"));  
 
  return html.join('');
}

//
// render for all claims for one doc 
// 


MOD_CLA.editclar=function() {
  var html=[];
  html.push("<TABLE border=2 CELLSPACING=0 id=bigcltable>");
  var pcl_iter=new PageClaim_Iterator();
  while(!pcl_iter.end()) {
    html.push("<div id=cldiv"+pcl_iter.getValue()+">");
    html.push(MOD_CLA.editclarcl(pcl_iter.getValue()));
    html.push("</div>"); 
    pcl_iter.next();
  }
  html.push("</TABLE>");
  return html.join(''); 
}

function tf_updatenotsearchedview() {
  ClaimToolNoOp();
}

MOD_CLA.tf_onchangetfimpver=function() {
  MOD_CLA.tfimpver   =html_selected_iter("tfimpver");
}

MOD_CLA.info=function() {
  var html="";
  html= html_warning("allwarn",(GetClaimsWarnings() >0 ) ? GetClaimsWarnings()+": warnings":"");
  return html;
}

MOD_CLA.extra=function() {
  var html="";
  if(GetClaimSetVersion()>1) {
    html+="<fieldset><legend>Import</legend>";
    html+="TF from V"+html_selection_iter("tfimpver",
					  new range_iterator(1,GetClaimSetVersion()-1),
					  MOD_CLA.tfimpver,"MOD_CTF.tf_onchangetfimpver()");
    html+="</fieldset>";
  }
  return html;
}

function edit_clarity(pn) {   
  ACT_MOD= MOD_CLA;

  if(!CLAIMSET) {
    alert("Create or Select a claim set first !");
    return;
  }

  // Perf mesure 1/2  
  //var start = new Date().getTime();

 
  if(GetClaimSetVersion()>1) {
    MOD_CLA.tfimpver   =html_selected_iter("tfimpver",MOD_CTF.tfimpver);
  } 
 
  ClaimToolEditFilterSection("Clarity",MOD_CLA.editclar(),"edit_clarity()",
			     "","","",MOD_CLA.info(),MOD_CLA.extra());
}

function printtfview() {
  var html="<TABLE class='LISTTXTCL' style='border-spacing:5px 40px;'>";
  var cl_iter =  new filteredclaims_iterator();
  while(!cl_iter.end()) {
   var tf_iter= new claim_features_iterator(cl_iter.getValue());
   html+="<TR><TD VALIGN=top class='LISTTXTCL'>"+cl_iter.getValue();
   html+=". </TD>";
   while(!tf_iter.end()) {
     html+="<TD style='border-right:1px solid black;spacing:1px 1px 10px 20px;'><small>"+tf_iter.gettext()+"</small></TD>";
     html+="<TD VALIGN=top class='LISTTXTCL' style='padding-left:10px;'>"; 
     switch(tf_iter.getUnclarity()) {
     case "added":
       html+="<u>"+tf_iter.getDesc()+"</u>";
       break;
     case "broad":
     case "vague":
       html+="<i>"+tf_iter.getDesc()+"</i>";
       break;
     default:
       html+=tf_iter.getDesc();
     }
     html+="</TD></TR><TR><TD></TD>";
     tf_iter.next();
   }   
   html+="<TD>&nbsp<BR>&nbsp</TD></TR>";
   cl_iter.next();
  }
  html+="</TABLE>";
  return html;
}
